package cn.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DB {
	public static Connection getConnection(String ip, String dbName,
			String user, String pwd) throws InstantiationException, IllegalAccessException {
		Connection conn = null;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//			 String URL = "jdbc:sqlserver://" + server + ":1433" + ";DatabaseName="
//		                + dbname;
			
			conn = DriverManager.getConnection("jdbc:sqlserver://" + ip + ":1433;DatabaseName="+dbName
					, user, pwd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;

	}
	public static Connection getConnection() {
//		Connection connectionSQLServer = getConnectionSQLSERVER("127.0.0.1", "tmall",
//				"sa", "sa123");
		Connection connection202 = null;
		try {
			connection202 = getConnection("localhost", "pro", "sa",
					"sa123");
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return connection202;
	}
	
	public static void insertProblems(Connection connection,Problem problem,Ans ans) {
		PreparedStatement pst;
		String sql="insert into problem (problem,a,b,c,d,an,ani) values (?,?,?,?,?,?,?)";
		 
		try {
			pst=connection.prepareStatement(sql);
			pst.setString(1, problem.getQuestion());
			pst.setString(2, problem.getA());
			pst.setString(3, problem.getB());
			pst.setString(4, problem.getC());
			pst.setString(5, problem.getD());
			pst.setString(6, ans.getAn());
			pst.setString(7, ans.getAni());
			
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void insertAns(Connection connection, Ans ans) {
		// TODO Auto-generated method stub
		PreparedStatement pst;
		String sql="insert into problem (an,ani) values(?,?)";
		try {
			pst=connection.prepareStatement(sql);
			pst.setString(1, ans.getAn());
			pst.setString(2, ans.getAni());
			pst.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
